import hashlib
from google.appengine.ext import db

from auth import SECRET

from suki.cloud import Datum

from suki.cloud.bot.cookies import Cookies

def CheckAuth(request):
  #site_domain = Datum.get('site_domain')
  site_domain = "ccdjh"
  cookies = Cookies(request, max_age = 86400, path = '/')
  if 'auth' in cookies:
    auth = cookies['auth']
    if str(auth) != hashlib.sha1(SECRET + ':' + site_domain).hexdigest():
      return False
    else:
      return True
  else:
    return False

def DoAuth(request, destination, message = None):
  if message != None:
    request.session['message'] = message
  else:
    request.session['message'] = 'Please sign in first'
  return request.redirect('/lock?destination=' + destination)



def CheckAuthSilence(request):
  #site_domain = Datum.get('site_domain')
  #site_domain = "ccdjh"
	cookies = Cookies(request, max_age = 86400, path = '/')
	if 'auth' in cookies:
		auth = cookies['auth']
		a = db.GqlQuery("SELECT * FROM Admin")
		if a.count() == 1:
			author_password = a[0].author_password
			author_name = a[0].author_name
		else:
			return False
		if str(auth) != hashlib.sha1(author_password + ':' + author_name).hexdigest():
			return False
		else:
			return True
	else:
		return False

def DoAuthSilence(request, destination, message = None):
  if message != None:
    request.session['message'] = message
  else:
    request.session['message'] = 'Please sign in first'
  return request.redirect('/lock?destinationn=' + destination)
